package org.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.backend.entity.Cart;
import org.example.backend.entity.WaterProduct;
import org.example.backend.mapper.CartMapper;
import org.example.backend.service.CartService;
import org.example.backend.service.WaterProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 购物车服务实现类
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    private WaterProductService waterProductService;

    @Override
    public List<Cart> listByUserId(Long userId) {
        List<Cart> cartList = baseMapper.selectCartListByUserId(userId);
        
        // 计算小计金额
        for (Cart cart : cartList) {
            if (cart.getProduct() != null) {
                BigDecimal price = cart.getProduct().getPrice();
                Integer quantity = cart.getQuantity();
                cart.setSubtotal(price.multiply(new BigDecimal(quantity)));
            }
        }
        
        return cartList;
    }

    @Override
    public boolean addToCart(Cart cart) {
        // 查询商品是否存在
        WaterProduct product = waterProductService.getById(cart.getProductId());
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        
        // 检查商品是否上架
        if (product.getStatus() == 0) {
            throw new RuntimeException("商品已下架");
        }
        
        // 检查库存是否充足
        if (product.getStock() < cart.getQuantity()) {
            throw new RuntimeException("商品库存不足");
        }
        
        // 查询购物车是否已存在该商品
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, cart.getUserId());
        wrapper.eq(Cart::getProductId, cart.getProductId());
        Cart existCart = getOne(wrapper);
        
        Date now = new Date();
        
        if (existCart != null) {
            // 更新数量
            existCart.setQuantity(existCart.getQuantity() + cart.getQuantity());
            existCart.setUpdateTime(now);
            return updateById(existCart);
        } else {
            // 新增购物车记录
            cart.setCreateTime(now);
            cart.setUpdateTime(now);
            return save(cart);
        }
    }

    @Override
    public boolean updateQuantity(Long id, Integer quantity) {
        // 查询购物车记录
        Cart cart = getById(id);
        if (cart == null) {
            throw new RuntimeException("购物车记录不存在");
        }
        
        // 查询商品
        WaterProduct product = waterProductService.getById(cart.getProductId());
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        
        // 检查库存是否充足
        if (product.getStock() < quantity) {
            throw new RuntimeException("商品库存不足");
        }
        
        // 更新数量
        cart.setQuantity(quantity);
        cart.setUpdateTime(new Date());
        
        return updateById(cart);
    }

    @Override
    public boolean removeFromCart(Long id) {
        return removeById(id);
    }

    @Override
    public boolean clearCart(Long userId) {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, userId);
        return remove(wrapper);
    }
} 